package com.lcredu.lcrliving.commodity.controller;

import java.util.Arrays;
import java.util.Map;

import com.lcredu.common.util.PageUtils;
import com.lcredu.common.util.R;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lcredu.lcrliving.commodity.vo.SpuSaveVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lcredu.lcrliving.commodity.entity.SpuInfoEntity;
import com.lcredu.lcrliving.commodity.service.SpuInfoService;


/**
 * 商品 spu 信息
 *
 * @author lcr
 * @email lcr@gmail.com
 * @date 2023-12-06 08:50:58
 */
@RestController
@RequestMapping("commodity/spuinfo")
public class SpuInfoController {

    /**
     * 商品spu上架
     */
    @RequestMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId) {
        spuInfoService.up(spuId);
        return R.ok();
    }

    /**
     * 商品spu下架
     */
    @RequestMapping("/{spuId}/down")
    public R downUp(@PathVariable("spuId") Long spuId) {
        spuInfoService.down(spuId);
        return R.ok();
    }

    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("commodity:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
//        PageUtils page = spuInfoService.queryPage(params);
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("commodity:spuinfo:info")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     * 因为我们保存的商品信息,涉及多表
     * 将SpuInfoEntity改成SpuSaveVO,进行数据保存
     * 拆解进行保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:spuinfo:save")
    public R save(@RequestBody SpuSaveVO spuSaveVO) {
//		spuInfoService.save(spuInfo);
        spuInfoService.saveSpuInfo(spuSaveVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:spuinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
